package com.example.demo.controller;

import com.example.demo.service.InitializationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/init")
public class InitializationController {

    @Autowired
    private InitializationService initializationService;

    private final String[] createTableString = {
        """
            CREATE TABLE IF NOT EXISTS `user` (
                `id` INTEGER PRIMARY KEY AUTOINCREMENT,
                `name` TEXT NOT NULL,
                `phoneNumber` CHARACTER(11) NOT NULL,
                `password` VARCHAR(20) NOT NULL,
                `role` TINYINT NOT NULL DEFAULT 100,
                `introduction` VARCHAR(128) NOT NULL DEFAULT '',
                `createTime` TEXT NOT NULL DEFAULT '',
                `updateTime` TEXT NOT NULL DEFAULT ''
            );
        """,
        // 添加其他表的创建语句
    };

    private final String[] insertInitData = {
        "INSERT INTO `user` (`name`, `phoneNumber`, `password`, `role`, `introduction`, `createTime`, `updateTime`) VALUES ('admin', '15715082893', '123456', 1, '这个人没有介绍～', '2021-07-01 00:00:00', '2021-07-01 00:00:00');"
    };

    @GetMapping("/createTable")
    public String createTable() {
        for (int i = 0; i < createTableString.length; i++) {
            int result = initializationService.createTable(createTableString[i]);
            if (result == 0) {
                return "创建表失败：" + i;
            }
        }
        return "所有表创建成功";
    }

    @GetMapping("/insertData")
    public String insertData() {
        for (String sql : insertInitData) {
            int result = initializationService.insertData(sql);
            if (result == 0) {
                return "数据插入失败";
            }
        }
        return "数据插入成功";
    }
}
